package com.gzgy.ustore.api.web.controller.webservice;

import com.gzgy.ustore.api.common.util.I18nUtil;
import com.gzgy.ustore.api.service.ParamTypeService;
import com.gzgy.ustore.api.util.ResultUtil;
import com.gzgy.ustore.api.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.sql.SQLException;

@Controller
public class ParamTypeController extends BaseController{

    @Autowired
    private ParamTypeService paramTypeService;

    @RequestMapping(value = "paramTypeList",method = RequestMethod.POST)
    public String paramTypeList(ModelMap modelMap,String userId,String session,String companyId,String index,String page,String paramKey,String type){

        Assert.hasText(companyId,"companyId " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(index,"index " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(page,"page " + I18nUtil.getMessage(-2,null,"zh_CN"));

        modelMap.put("message", ResultUtil.getResultJson(paramTypeService.paramTypeList(companyId,index,page,paramKey,type)));

        return SHOW_JSON_NAME;
    }

    @RequestMapping(value = "addParamType",method = RequestMethod.POST)
    public String addParamType(ModelMap modelMap,String userId,String session,String companyId,String paramType,String paramKey,String paramValue,String desc){

        Assert.hasText(companyId,"companyId " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(paramType,"paramType " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(paramKey,"paramKey " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(paramValue,"paramValue " + I18nUtil.getMessage(-2,null,"zh_CN"));

        modelMap.put("message", ResultUtil.getResultJson(paramTypeService.addParamType(paramType,paramKey,companyId,userId,paramValue,desc)));

        return SHOW_JSON_NAME;
    }

    @RequestMapping(value = "paramTypeOperation",method = RequestMethod.POST)
    public String updateParamType(ModelMap modelMap,String userId,String session,String flag,String paramId,String paramValue,String desc) throws SQLException {

        Assert.hasText(flag,"flag " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(paramId,"paramId " + I18nUtil.getMessage(-2,null,"zh_CN"));
        if("0".equals(flag)){
            Assert.hasText(paramValue,"paramValue " + I18nUtil.getMessage(-2,null,"zh_CN"));
        }
        modelMap.put("message", ResultUtil.getResultJson(paramTypeService.updateParamType(flag,userId,paramId,paramValue,desc)));

        return SHOW_JSON_NAME;
    }
}
